/**
 * 初始化冲泡方式详情对话框
 */
var BrewsInfoDlg = {
    brewsInfoData: {},
    validateFields: {
        name: {
            validators: {
                notEmpty: {
                    message: '冲泡名称不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
BrewsInfoDlg.clearData = function () {
    this.brewsInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
BrewsInfoDlg.set = function (key, val) {
    this.brewsInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
BrewsInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 表单数据校验
 * @returns {*|jQuery}
 */
BrewsInfoDlg.validate = function () {
    $('#brewsInfoForm').data("bootstrapValidator").resetForm();
    $('#brewsInfoForm').bootstrapValidator('validate');
    return $("#brewsInfoForm").data('bootstrapValidator').isValid();
};

/**
 * 关闭此对话框
 */
BrewsInfoDlg.close = function () {
    parent.layer.close(window.parent.Brews.layerIndex);
}

/**
 * 收集数据
 */
BrewsInfoDlg.collectData = function () {
    this
        .set('id')
        .set('name')
        .set('teapot')
        .set('temperature')
        .set('percent')
        .set('step1')
        .set('step2')
        .set('step3')
        .set('step4')
        .set('step5')
        .set('details')
        .set('status')
        .set('crtUser')
    ;
}

/**
 * 提交添加
 */
BrewsInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/brews/add", function (data) {
        //新增之后关联选择的图片
        BrewsInfoDlg.selector(data.id);
        window.parent.Brews.table.refresh();
        BrewsInfoDlg.close();
    }, function (data) {
        //名称已经存在
        if (data.responseJSON.code == "410") {
            $(".has-feedback").addClass("has-error");
        }
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.brewsInfoData);
    ajax.start();
};

/**
 * 关联图片
 */
BrewsInfoDlg.selector = function (brewsId) {
    for(var i=0;i<5;i++){
        var selId = $('#img' + i).attr('selId');
        if(selId){
            var ajax = new $ax(Feng.ctxPath + "/brews/selector", function (data) {
                Feng.success("添加成功!");
            }, function (data) {
                Feng.error("关联图片失败!" + data.responseJSON.message + "!");
            });
            //冲泡方式的id
            ajax.set("brewsId", brewsId);
            ajax.set("sequence",i);
            ajax.set("ids", selId);
            ajax.start();
        }
    }
};

/**
 * 取消之前关联的图片
 */
BrewsInfoDlg.cancelSelector = function () {
    var ajax = new $ax(Feng.ctxPath + "/picture/cancelSelector", function (data) {
        console.log("取消关联成功");
    }, function () {
        console.log("取消关联失败");
    });
    ajax.set("type","brews");
    ajax.set("itemId", $("#id").val());
    ajax.start();
}

/**
 * 提交修改
 */
BrewsInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/brews/update", function (brews) {
        //关联图片前先取消关联
        BrewsInfoDlg.cancelSelector();
        //取消关联之后关联新图片
        BrewsInfoDlg.selector(brews.id);
        Feng.success("修改成功!");
        window.parent.Brews.table.refresh();
        BrewsInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.brewsInfoData);
    ajax.set("details", BrewsInfoDlg.editor.txt.html().toString());
    ajax.start();
};



/**
 * 打开图片选择框
 */
BrewsInfoDlg.openPictureSelector = function (sequence) {
    //判断是新增页面还是编辑页面
    var brewsId = $('#id').val();
    var content = Feng.ctxPath + '/brews/brews_picture?brewsId=' + brewsId + "&sequence=" + sequence ;
    //得到冲泡方式的id
    var index = layer.open({
        type: 2,
        title: '选择关联图片',
        area: ['1300px', '620px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: content
    });
    this.layerIndex = index;
};

$(function () {
    Feng.initValidator("brewsInfoForm", BrewsInfoDlg.validateFields);
    //初始化富文本框编辑器
    var E = window.wangEditor;
    var editor = new E('#editor');
    //设置图片的上传地址
    editor.customConfig.debug = true;
    //以base64的格式存储
    editor.customConfig.uploadImgShowBase64 = true;
    //以文件形式上传到服务器
    //editor.customConfig.uploadFileName = 'file';
    //editor.customConfig.uploadImgServer = '/picture/upload';
    editor.create();
    //编辑框宽度设置
    $(".w-e-text-container").css("height", "500px");
    //判断是新增页面还是编辑页面
    var details = $("#details").html()
    //if(!details && details != ""){
    editor.txt.html(details);
    //}
    BrewsInfoDlg.editor = editor;
});
